package com.googlecode.payroll

import org.hibernate.*

import static com.googlecode.payroll.util.RoleUtil.*
import static com.googlecode.payroll.util.DateUtil.*

class WorkingHoursTotalizerCreateService {

    def sessionFactory

    def create(String month) {

        Date startOfMonth = startOfMonth(month)
        Date endOfMonth = endOfMonth(month)

        String totalHoursQureryStatement = "select sum(timeCard.hours), timeCard.employee.id " + 
                                           "from TimeCard timeCard " + 
                                           "where date >= :startOfMonth and :endOfMonth >= date " +
                                           "group by timeCard.employee.id"

        def session = sessionFactory.getCurrentSession()
        Query totalHoursQuery = session.createQuery(totalHoursQureryStatement)
        totalHoursQuery.setParameter("startOfMonth", startOfMonth)
        totalHoursQuery.setParameter("endOfMonth", endOfMonth)

        def records = totalHoursQuery.list()
        assign(records, WorkingHoursTotalizer)
    }

}
